<template>
  <div class="container">
    <div class="content">
      <div class="header">黑马面经运营后台</div>
      <div class="form">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-position="top"
          label-width="80px"
        >
          <el-form-item label-position="top" label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="submitForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { loginApi } from '@/api/user'
export default {
  name: 'LoginView',
  props: {},
  components: {},
  data () {
    return {
      ruleForm: {
        username: 'admin',
        password: 'admin'

      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,11}$/, message: '请输入5-11位字母数字下划线', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) {
          return
        }
        // 请求
        const { data: { token } } = await loginApi(this.ruleForm)
        // console.log(token)
        this.$store.commit('setTokenFn', token)
        this.$router.replace(this.$route.query.redirect || '/home')
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  position: relative;
  height: 100vh;
  background-image: url("~@/assets/login-bg.svg");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  .content {
    position: absolute;
    width: 420px;
    height: 420px;
    background-color: #fff;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .header {
      height: 80px;
      background-color: rgb(114, 124, 245);
      text-align: center;
      line-height: 80px;
      color: #fff;
      font-size: 25px;
    }
    .form {
      padding: 40px;
    }
    ::v-deep .el-form-item__content {
      text-align: center;
    }
  }
}
</style>
